package cn.ctodb.cms.service;

import cn.ctodb.cms.domain.CmsChannel;
import cn.ctodb.cms.repository.CmsChannelRepository;
import cn.ctodb.cms.repository.search.CmsChannelSearchRepository;
import cn.ctodb.cms.service.dto.CmsChannelDTO;
import cn.ctodb.cms.service.mapper.CmsChannelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing CmsChannel.
 */
@Service
@Transactional
public class CmsChannelService {

    private final Logger log = LoggerFactory.getLogger(CmsChannelService.class);
    
    @Inject
    private CmsChannelRepository cmsChannelRepository;

    @Inject
    private CmsChannelMapper cmsChannelMapper;

    @Inject
    private CmsChannelSearchRepository cmsChannelSearchRepository;

    /**
     * Save a cmsChannel.
     *
     * @param cmsChannelDTO the entity to save
     * @return the persisted entity
     */
    public CmsChannelDTO save(CmsChannelDTO cmsChannelDTO) {
        log.debug("Request to save CmsChannel : {}", cmsChannelDTO);
        CmsChannel cmsChannel = cmsChannelMapper.cmsChannelDTOToCmsChannel(cmsChannelDTO);
        cmsChannel = cmsChannelRepository.save(cmsChannel);
        CmsChannelDTO result = cmsChannelMapper.cmsChannelToCmsChannelDTO(cmsChannel);
        cmsChannelSearchRepository.save(cmsChannel);
        return result;
    }

    /**
     *  Get all the cmsChannels.
     *  
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public List<CmsChannelDTO> findAll() {
        log.debug("Request to get all CmsChannels");
        List<CmsChannelDTO> result = cmsChannelRepository.findAll().stream()
            .map(cmsChannelMapper::cmsChannelToCmsChannelDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one cmsChannel by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public CmsChannelDTO findOne(Long id) {
        log.debug("Request to get CmsChannel : {}", id);
        CmsChannel cmsChannel = cmsChannelRepository.findOne(id);
        CmsChannelDTO cmsChannelDTO = cmsChannelMapper.cmsChannelToCmsChannelDTO(cmsChannel);
        return cmsChannelDTO;
    }

    /**
     *  Delete the  cmsChannel by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete CmsChannel : {}", id);
        cmsChannelRepository.delete(id);
        cmsChannelSearchRepository.delete(id);
    }

    /**
     * Search for the cmsChannel corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<CmsChannelDTO> search(String query) {
        log.debug("Request to search CmsChannels for query {}", query);
        return StreamSupport
            .stream(cmsChannelSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(cmsChannelMapper::cmsChannelToCmsChannelDTO)
            .collect(Collectors.toList());
    }
}
